//***************************************************************************
// A P P L I C A T I O N   N O T E   F O R   T H E   A V R   F A M I L Y
//
// Number               : AVR151
// File Name            : "avr151_Master.c"
// Title                : Setup and Use The SPI
// Date                 : 00.09.20
// Version              : 1.0
// Target MCU           : Any AVR with SPI
//
// DESCRIPTION
// This Application note shows how to enable and use the on-board SPI of the 
// AVR's in Master- and in Slave-Mode. 
//
// This file contains the code of all the examples. To switch between the examples
// you will have to enable the call of the corresponding routines.
// (see the comments in the main program at the bottom)
//
// Modified 2004-10-25 RAA
//
//**************************************************************************

#include "inavr.h"
#include "ioavr.h"

char*    SENDString;   
char*    GETString;
char*    PtrToSendStrChar;                  	// Pointer to certain Character in String
char*    PtrToGetStrChar;
char     ClearToSend   = 1;             	// String send complete bit
/*
// Interrupt Routine Master Mode (interrupt controlled)
#pragma vector=SPI_STC_vect
__interrupt void ISR_SPI (void)
{
        PtrToSendStrChar++;                 	// Point to next Char in String
        if (*PtrToSendStrChar != 0)         	// if end not reached
        {
                SPDR  = *PtrToSendStrChar;  	// send Character
        }
        else ClearToSend = 1;           	// if end reached enable transmission of next String
}
*/
// Intialization Routine Master Mode (polling)
void Init_Master (void)
{
        volatile char IOReg;
        // set SPI_CS_ADC(/SS), SPI_MOSI(MOSI), SPI_SCK(SCK) as output 
        SPI_DDRX = (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS)|(1<<SPI_CS_DP1)|(1<<SPI_CS_DP2)|(1<<SPI_CS_ADC)|(0<<SPI_MISO);
		DDR_D = (0<<RXD)|(0<<ADC_BUSSY)|(1<<TXD);
        // enable SPI in Master Mode with SCK = CK/16
        SPCR    = (1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0);
        IOReg   = SPSR;                 	// clear SPIF bit in SPSR
        IOReg   = SPDR;
}
/*
// Intialization Routine Master Mode (interrupt controlled)
void Init_Master_IntContr (void)
{
        volatile char IOReg;
        // set PB4(/SS), PB5(MOSI), PB7(SCK) as output 
        DDRB  = (1<<PB4)|(1<<PB5)|(1<<PB7);
        // enable SPI Interrupt and SPI in Master Mode with SCK = CK/16
        SPCR  = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR0);
        IOReg   = SPSR;                 	// clear SPIF bit in SPSR
        IOReg   = SPDR;
        __enable_interrupt();
}

*/

// Sending Routine Master Mode (polling)
void Master_Send (unsigned char num, unsigned char *send_data,/* unsigned char *get_data,*/ char CS)
{	
		
        unsigned char *get_data  
		SPI_PORTX &= ~(1<<CS);
        while   (num--)            					
        {
                SPDR  = *send_data;          		// send Character
                while (!(SPSR & (1<<SPIF)));   		// wait until Char is sent 
				*send_data++
				*get_data++ = SPDR;;                

        }

		SPI_PORTX |= (1<<CS);
		return *get_data;
}

/*
// Sending Routine Master Mode (interrupt controlled)
void Master_Send_IntContr (void)
{
        if (ClearToSend == 1){                  // if no transmission is in progress
                PtrToSendStrChar  = TextString;     // Set Pointer to beginning of String
                                                // initiate new transmission by 
                SPDR = *PtrToSendStrChar;           // sending first Char of new String
                ClearToSend = 0;                // block initiation of new transmissions
        }
}

*/